package tourist.springboot.web;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import tourist.springboot.bean.ResponseData;
import tourist.springboot.domain.TouristAttraction;
import tourist.springboot.service.TouristAttractionService;
import org.springframework.beans.factory.annotation.Autowired;
import tourist.springboot.utils.IDGenerateUtils;
import tourist.springboot.utils.ResponseDataUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author wangzg
 * 旅游景点控制器
 */
@RestController
public class TouristAttractionController {

    @Autowired
    private TouristAttractionService touristAttractionService;

    /**
     * 获取旅游景点列表
     * @return
     */
    @ApiOperation(value = "查询旅游景点列表", notes = "查询旅游景点列表")
    @GetMapping("/tourist_attraction/list")
    public List<TouristAttraction> touristAttractionList(){
        return touristAttractionService.findAll();
    }

    /**
     * 查看旅游景点详细信息
     * @param id
     * @return
     */
    @GetMapping("/tourist_attraction/{id}")
    public ResponseData getTouristAttractionById(@PathVariable("id") String id){
        if(StringUtils.isNotEmpty(id)){
            return ResponseDataUtils.successResponse(touristAttractionService.findById(id));
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 新增旅游景点信息
     * @param touristAttraction
     * @return
     */
    @PostMapping("/tourist_attraction")
    public ResponseData addTouristAttraction(@RequestBody TouristAttraction touristAttraction){
        if(null != touristAttraction){
            touristAttraction.setId(IDGenerateUtils.generateId());
            touristAttractionService.insert(touristAttraction);
            return ResponseDataUtils.successResponse();
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 修改旅游景点信息
     * @param touristAttraction
     * @return
     */
    @PutMapping("/tourist_attraction")
    public ResponseData updateTouristAttraction(@RequestBody TouristAttraction touristAttraction){
        if(null != touristAttraction){
            touristAttractionService.update(touristAttraction);
            return ResponseDataUtils.successResponse();
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 删除旅游景点
     * @param id
     * @return
     */
    @DeleteMapping("/tourist_attraction/{id}")
    public ResponseData deleteTouristAttraction(@PathVariable("id") String id){
        if(StringUtils.isNotEmpty(id)){
            touristAttractionService.delete(id);
            return ResponseDataUtils.successResponse();
        }
        return ResponseDataUtils.errorResponse();
    }

    /**
     * 获取旅游景点相关推荐
     * @param id
     * @return
     */
    @GetMapping("/tourist_attraction/{id}/recommend")
    public ResponseData getRecommendListByTouristAttraction(@PathVariable("id") String id){
        if(StringUtils.isNotEmpty(id)){
            TouristAttraction touristAttraction = touristAttractionService.findById(id);
            if(null != touristAttraction){
                String recommendIdStr = touristAttraction.getRecommend();
                if(StringUtils.isNotEmpty(recommendIdStr)){
                    List<TouristAttraction> recommendList = touristAttractionService.findByIds(Arrays.asList(recommendIdStr.split(",")));
                    return ResponseDataUtils.successResponse(recommendList);
                }
            }

        }
        return ResponseDataUtils.errorResponse();
    }
}
